package cn.bornsoft.mobileServer.servlet;

import it.sauronsoftware.base64.Base64;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.bornsoft.mobileServer.db.JkNote;
import cn.bornsoft.mobileServer.logic.ServiceLogic;
import net.sf.json.*;

public class TService extends HttpServlet {
	static Logger log = Logger.getLogger(TService.class);

	/**
	 * Constructor of the object.
	 */
	public TService() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html; charset=utf-8");
		// response.setContentType("text/html; charset=utf-8");
		// response.setContentType("application/x-json");// 需要设置ContentType
		// 为"application/x-json"

		String jsoncallback = request.getParameter("jsoncallback");
		String type = request.getParameter("type");
		JSONObject resultJSON = new JSONObject();// 构建一个JSONObject
		String encoded = Base64.encode("你好，我是中文", "UTF-8");
		String decoded = Base64.decode(encoded, "UTF-8");

		if (type.equals("addNote")) {
			ServiceLogic tServiceLogic = new ServiceLogic();
			JkNote jkNote = new JkNote();
			String title = request.getParameter("title");
			title = Base64.decode(title,"GBK");
			String name = request.getParameter("name");
			name = Base64.decode(name,"GBK");
			String email = request.getParameter("email");
			email = Base64.decode(email,"GBK");
			String tel = request.getParameter("tel");
			tel = Base64.decode(tel,"GBK");
			String address = request.getParameter("address");
			address = Base64.decode(address,"GBK");
			String content = request.getParameter("content");
			content = Base64.decode(content,"GBK");
			if (title.equals("") || name.equals("") || content.equals("")
					|| email.equals("")) {
				resultJSON.accumulate("errNum", 0);
				resultJSON.accumulate("errInfo", "请填上必选项!");
			} else {
				jkNote.setTitle(title);
				jkNote.setName(name);
				jkNote.setEmail(email);
				jkNote.setTel(tel);
				jkNote.setAdress(address);
				jkNote.setContent(content);
				Date date = new Date();
				Timestamp time = new Timestamp(date.getTime());
				jkNote.setAdddate(time);
				jkNote.setState(new Long(1));
				jkNote.setIsdelete(new Long(0));
				String ipaddress = request.getRemoteHost();
				jkNote.setIp(ipaddress);
				jkNote = tServiceLogic.addNote(jkNote);
				if (!jkNote.getId().equals(null)) {
					resultJSON.accumulate("errNum", 1);
					resultJSON.accumulate("errInfo", "提交成功，我们将尽快回复!");
				}
			}
		}
		PrintWriter out = response.getWriter();
		out.println(jsoncallback + "(" + resultJSON.toString() + ")");// 向客户端输出JSONObject字符串
		log.debug(jsoncallback + "(" + resultJSON.toString() + ")");
		out.flush();
		out.close();

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
